package com.classic.duanzhi;


import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.classic.adapter.ListDetailsAdapter;
import com.classic.base.BaseActivity;
import com.classic.content.contents;
import com.classic.utils.NoSlideViewPager;

import java.util.ArrayList;
import java.util.List;

public class UDetailsActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
    private static final String DATA = "DATA_";
    private static final String DATA_title = "DATA_title";

    public static void starThis(FragmentActivity activity, DZDataBen1 dataBen1, String title) {
        Intent intent = new Intent(activity, UDetailsActivity.class);
        String url = dataBen1.getUrl();
        if (!url.contains("http")) {
            url = "http://www.haha56.net" + dataBen1.getUrl();
        }
        intent.putExtra(DATA, url);
        intent.putExtra(DATA_title, title);
        activity.startActivity(intent);
    }
    public static void starThis(FragmentActivity activity, DZDataBen1 dataBen1) {
        Intent intent = new Intent(activity, UDetailsActivity.class);
        String url = dataBen1.getUrl();
        if (!url.contains("http")) {
            url = "http://www.haha56.net" + dataBen1.getUrl();
        }
        intent.putExtra(DATA, url);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_udetalis;
    }

    NoSlideViewPager pager;
    TextView up;
    TextView next;

    @Override
    protected void initView() {
        pager = findViewById(R.id.vp_pager);
        up = findViewById(R.id.tv_up);
        next = findViewById(R.id.tv_next);
        up.setOnClickListener(getOnClick());
        next.setOnClickListener(getOnClick());
        String url = getIntent().getStringExtra(DATA);
        mUrl.add(url);
        pager.setPagingEnabled(false);
        pager.setAdapter(new ListDetailsAdapter(getSupportFragmentManager()));
        pager.addOnPageChangeListener(this);
    }

    public List<String> mUrl = new ArrayList();

    @Override
    protected void initData() {

    }

    @Override
    public String getActivityTitle() {
        return getIntent().getStringExtra(DATA_title);
    }

    public View.OnClickListener getOnClick() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = v.getId();
                if (i == R.id.tv_next) {
                    pager.setCurrentItem(index + 1);
                } else if (i == R.id.tv_up) {
                    pager.setCurrentItem(index - 1);
                }

            }
        };
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    private int index;

    @Override
    public void onPageSelected(int position) {
        index = position;
        up.setVisibility(position == 0 ? View.GONE : View.VISIBLE);
        next.setVisibility(position == (mUrl.size() - 1) ? View.GONE : View.VISIBLE);
        pager.setPagingEnabled(position != (mUrl.size() - 1));
    }

    /**
     * 显示下一个的按钮 打开开关
     *
     * @param url
     */
    public void addUrl(final String url) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (url.contains("http")) {
                    mUrl.add(url);
                } else {
                    mUrl.add(contents.url);
                }


                if (null != next) {
                    next.setVisibility(View.VISIBLE);
                    pager.setPagingEnabled(true);
                }
            }
        });

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public String getUrl() {
        return mUrl.get(index);
    }
}
